/**
 * Copyright &copy; 2012-2013 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.chlitina.store.modules.sys.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.chlitina.store.common.service.BaseService;
import com.chlitina.store.modules.sys.dao.SettingsDao;
import com.chlitina.store.modules.sys.dao.StoreDao;
import com.chlitina.store.modules.sys.dao.StoreLoginDao;
import com.chlitina.store.modules.sys.dao.StoreMenuDao;
import com.chlitina.store.modules.sys.entity.Settings;
import com.chlitina.store.modules.sys.entity.Store;
import com.chlitina.store.modules.sys.entity.StoreMenu;
import com.chlitina.store.modules.sys.security.SystemRealm;
import com.chlitina.store.modules.sys.utils.StoreUtils;

/**
 * 系统管理，安全相关实体的管理类,包括用户、角色、菜单.
 * @author ThinkGem
 * @version 2013-4-19
 */
@Service
@Transactional(readOnly = true)
public class SystemService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(SystemService.class);
	
	@Autowired
	private StoreDao storeDao;
	@Autowired
	private StoreMenuDao storeMenuDao;
	@Autowired
	private SystemRealm systemRealm;
	@Autowired
	private SettingsDao settingsDao;
	@Autowired
	private StoreLoginDao storeLoginDao;
	
	//-- Store Service --//
	/**
	 * 通过登录名查询门店
	 */
	@Transactional(readOnly = false)
	public Store getStoreByLoginName(String name) {
		Store store = storeDao.getByName(name);
		return store;
	}
	
	@Transactional(readOnly = false)
	public void save(Store store) {
		storeDao.save(store);
		systemRealm.clearCachedAuthorizationInfo(store.getLoginName());
	}
	
	//-- Menu Service --//
	
	public StoreMenu getMenu(Long id) {
		return storeMenuDao.findOne(id);
	}

	public List<StoreMenu> findAllMenu(){
		return StoreUtils.getMenuList();
	}
	
	@Transactional(readOnly = false)
	public void saveMenu(StoreMenu menu) {
		menu.setParent(this.getMenu(menu.getParent().getId()));
		String oldParentIds = menu.getParentIds(); // 获取修改前的parentIds，用于更新子节点的parentIds
		menu.setParentIds(menu.getParent().getParentIds()+menu.getParent().getId()+",");
		storeMenuDao.clear();
		storeMenuDao.save(menu);
		// 更新子节点 parentIds
		List<StoreMenu> list = storeMenuDao.findByParentIdsLike("%,"+menu.getId()+",%");
		for (StoreMenu e : list){
			e.setParentIds(e.getParentIds().replace(oldParentIds, menu.getParentIds()));
		}
		storeMenuDao.save(list);
		systemRealm.clearAllCachedAuthorizationInfo();
	}

	@Transactional(readOnly = false)
	public void deleteMenu(Long id) {
		storeMenuDao.deleteById(id, "%,"+id+",%");
		systemRealm.clearAllCachedAuthorizationInfo();
	}
	
	public void updateLogin(String storeId, int num){
		Store store =  storeDao.findOne(Long.parseLong(storeId));
		store.setCurrent_amount(store.getCurrent_amount() + num);
		storeDao.save(store);
	}
	
	public void clearCurrentLogin(){
		List<Store> stores = (List<Store>) storeDao.findAll();
		for (Store store : stores) {
			store.setCurrent_amount(0);
			storeDao.save(store);
		}
	}
	
	public String getRootPassWord(){
		Settings settings = settingsDao.findByKey("SYS_ROOT_PASSWORD");
		return settings.getStringValue();
	}
}
